local addonName = "Portal"
_G[addonName] = LibStub('AceAddon-3.0'):NewAddon(addonName, "AceConsole-3.0")
local Portal = _G[addonName]
local db

function Portal:isMapCanSaved(uiMapID)
  local map = db.mapData[uiMapID]
  if map then
    self.Print("|cFFFFA500地图已存在:"..map.name.."_"..uiMapID.."|r")
    return false
  end
  return true
end

function Portal:isTaxiCanSaved(nodeID)
  local taxiNode = db.taxiData[nodeID]
  if taxiNode then
    self.Print("|cFFFFA500鸟点已存在:"..taxiNode.name.."_"..nodeID.."|r")
    return false
  end
  return true
end

-- 地图保存逻辑
function Portal:SaveMapData(mapInfo)
  if (#mapInfo == 0) then
    if self:isMapCanSaved(mapInfo.mapID) then
      db.mapData[mapInfo.mapID] = mapInfo
      self.Print("|cFF00FF00已保存: "..mapInfo.name.."+"..mapInfo.mapID)
    end
  else
    print("多个地图")
    for k, v in ipairs(mapInfo) do
      if self:isMapCanSaved(v.mapID) then
        db.mapData[v.mapID] = v
        self.Print("|cFF00FF00已保存: "..v.name.."+"..v.mapID)
      end
    end
  end
end

-- 鸟点保存逻辑
function Portal:SaveTaxiData(taxiNodes)
  for k, v in ipairs(taxiNodes) do
    if self:isTaxiCanSaved(v.nodeID) then
      db.taxiData[v.nodeID] = v
      self.Print("|cFF00FF00鸟点已保存: "..v.name.."+"..v.nodeID)
    end
  end
end

function Portal:GetMapData()
  return db.mapData
end

function Portal:OnInitialize()
  self.db = LibStub("AceDB-3.0"):New("PortalDB", {
    profile = {
      -- { [uiMapID] = { name, mapType, ... } }
      mapData = {},
      taxiData = {}
    }
  })
  db = self.db.profile
  self:Print("|cFF00FF00插件已加载，输入 /f 打开界面|r")
  self:RegisterChatCommand("f", function()
    -- 调用MainFrame中的函数
    PortalMainFrame:Create()
  end)
end